package com.indeep.user.controller;

import com.indeep.common.result.JsonResult;
import com.indeep.common.result.StatusCode;
import com.indeep.user.annotation.ControllerLog;
import com.indeep.user.dto.CodeDTO;
import com.indeep.user.service.AppUserService;
import com.indeep.user.service.ILoginService;
import com.indeep.user.vo.AppConfigVO;
import com.indeep.user.vo.CheckCodeVO;
import com.indeep.user.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/userLogin")
public class LoginController {
    
    @Autowired
    ILoginService loginService;
    
    @Autowired
    RedisTemplate redisTemplate;
    
    @Autowired
    private AppUserService appUserService;
    
    /**
     * 发送验证码
     *
     * @param phoneNumber
     * @return
     */
    @PostMapping(value = "jiguang/sendCode")
    public JsonResult sendCode(@RequestParam("phoneNumber") String phoneNumber) {
        loginService.sendCode(phoneNumber);
        return new JsonResult<>(StatusCode.SUCCESS);
    }
    
    /**
     * 登录时校验验证码
     *
     * @param codeDto
     * @return
     */
    @RequestMapping("/jiguang/checkCode")
    public JsonResult<CheckCodeVO> checkCode(@RequestBody CodeDTO codeDto) {
        CheckCodeVO res = loginService.checkCode(codeDto.getPhoneNumber(), codeDto.getCode());
        if (res != null && res.getId() == -1L) return new JsonResult<>(StatusCode.SUCCESS, res);
        if (res == null) return new JsonResult<>(StatusCode.CODE_VERIFICATION_FAILED);
        if (res.getRefreshToken() == null) return new JsonResult<>(StatusCode.USER_BANDED, res);
        return new JsonResult<>(StatusCode.SUCCESS, res);
    }
    
    @ControllerLog(description = "注册时填写邀请码")
    @RequestMapping("/checkInviteCode")
    public JsonResult<CheckCodeVO> checkInviteCode(@RequestBody CodeDTO dto) {
        CheckCodeVO res = loginService.checkInviteCode(dto);
        if (res == null) return new JsonResult<>(StatusCode.I_CODE_VERIFICATION_FAILED);
        if (res.getId() == -1L) return new JsonResult<>(StatusCode.I_CODE_CUR_NUM_LACK);
        if (res.getId() == -2L) return new JsonResult<>(StatusCode.I_CODE_EXPIRED);
        if (res.getId() == -3L) return new JsonResult<>(StatusCode.TEL_REGISTERED);
        return new JsonResult<>(StatusCode.SUCCESS, res);
    }
    
    @ControllerLog(description = "登录统计")
    @RequestMapping("/ydUser/login")
    public JsonResult<DataVO> login() {
        AppConfigVO vo = appUserService.login();
        return new JsonResult<>(StatusCode.SUCCESS, new DataVO(vo));
    }
    
    @ControllerLog(description = "退出登录")
    @RequestMapping("/yindi/logout")
    public JsonResult<String> logout() {
        loginService.logout();
        return new JsonResult<>(StatusCode.SUCCESS);
    }
    
    @ControllerLog(description = "注销账号时发送短信")
    @RequestMapping("/yindi/unsubscribe")
    public JsonResult<Object> unsubscribe() {
        loginService.unsubscribe();
        return new JsonResult<>(StatusCode.SUCCESS);
    }
    
    @ControllerLog(description = "注销账号时校验验证码")
    @RequestMapping("/yindi/unsubscribe/check")
    public JsonResult<Object> unsubscribeCheckCode(@RequestBody CodeDTO codeDto) {
        loginService.unsubscribeCheck(codeDto.getCode());
        return new JsonResult<>(StatusCode.SUCCESS);
    }
    
    @ControllerLog(description = "换绑手机号时给原手机号发送短信")
    @RequestMapping("/yindi/changeTel")
    public JsonResult<Object> changeTel() {
        loginService.changeTel();
        return new JsonResult<>(StatusCode.SUCCESS);
    }
    
    @ControllerLog(description = "换绑手机号时校验原手机号验证码")
    @RequestMapping("/yindi/changeTel/check")
    public JsonResult<Object> changeTelCheckCode(@RequestBody CodeDTO codeDto) {
        loginService.changeTelCheckCode(codeDto);
        return new JsonResult<>(StatusCode.SUCCESS);
    }
    
    @ControllerLog(description = "换绑手机号时给新手机号发送短信")
    @RequestMapping("/yindi/newTel")
    public JsonResult<Object> newTel(@RequestBody CodeDTO codeDto) {
        loginService.newTel(codeDto);
        return new JsonResult<>(StatusCode.SUCCESS);
    }
    
    @ControllerLog(description = "换绑手机号时校验新手机号验证码")
    @RequestMapping("/yindi/newTel/check")
    public JsonResult<Object> newTelCheckCode(@RequestBody CodeDTO codeDto) {
        loginService.newTelCheckCode(codeDto);
        return new JsonResult<>(StatusCode.SUCCESS);
    }
    
    @ControllerLog(description = "刷新身份令牌")
    @RequestMapping("/yindi/refreshToken")
    public JsonResult<CheckCodeVO> refreshToken() {
        CheckCodeVO res = loginService.refreshAccessToken();
        if (res == null) return new JsonResult<>(StatusCode.USER_LATEST_ACTIVE_TIME_NOT_EXISTS);
        return new JsonResult<>(StatusCode.SUCCESS, res);
    }
    
    @ControllerLog(description = "获取微信授权")
    @RequestMapping("/yindi/wechat/auth")
    public JsonResult<CheckCodeVO> wechatAuth(@RequestParam("code") String code) {
        CheckCodeVO checkCodeVO = loginService.wechatAuth(code);
        return new JsonResult<>(StatusCode.SUCCESS, checkCodeVO);
    }
    
    @ControllerLog(description = "绑定微信")
    @RequestMapping("/yindi/wechat/authAndBind")
    public JsonResult<CheckCodeVO> authAndBind(@RequestParam("code") String code) {
        loginService.authAndBind(code);
        return new JsonResult<>(StatusCode.INTERNAL_SERVER_ERROR);
    }
    
    @ControllerLog(description = "解绑微信")
    @RequestMapping("/yindi/wechat/unbind")
    public JsonResult<CheckCodeVO> unbindWechat() {
        loginService.unbindWechat();
        return new JsonResult<>(StatusCode.INTERNAL_SERVER_ERROR);
    }
    
    @ControllerLog(description = "微信登录绑定手机号发送验证码")
    @RequestMapping("/yindi/wechat/bindTel")
    public JsonResult<Integer> bindTel(@RequestBody CodeDTO dto) {
        loginService.bindTel(dto);
        return new JsonResult<>(StatusCode.SUCCESS);
    }
    
    @ControllerLog(description = "微信登录绑定手机号校验验证码")
    @RequestMapping("/yindi/wechat/bindTel/check")
    public JsonResult<CheckCodeVO> bindTelCheck(@RequestBody CodeDTO dto) {
        CheckCodeVO res = loginService.bindTelCheck(dto);
        if (res != null && res.getId() == -1L) return new JsonResult<>(StatusCode.SUCCESS, res);
        if (res == null) return new JsonResult<>(StatusCode.CODE_VERIFICATION_FAILED);
        if (res.getId() == -401) return new JsonResult<>(StatusCode.VX_AUTH_NOT_EXIST, res);
        if (res.getId() == -402) return new JsonResult<>(StatusCode.WX_BOUND, res);
        if (res.getRefreshToken() == null) return new JsonResult<>(StatusCode.USER_BANDED, res);
        return new JsonResult<>(StatusCode.SUCCESS, res);
    }
    
}
